package m.jclecai.com.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotOrderDetailActivity;
import m.jclecai.com.bean.Response.ResHotPeopleInfo;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class AdapterFragment extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public List<ResHotPeopleInfo.HotPeopleOrderBean> mData = new ArrayList<>();
    private LayoutInflater inflater;

    public AdapterFragment(Activity context, List<ResHotPeopleInfo.HotPeopleOrderBean> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_hot_people_info, parent, false);
        HotPeopleViewHolder holder = new HotPeopleViewHolder(view);
        return holder;
    }

    public void refresh(List<ResHotPeopleInfo.HotPeopleOrderBean> datas) {
        if (mData != null) {
            mData.clear();
            addData(datas);
        }
    }


    public void addData(List<ResHotPeopleInfo.HotPeopleOrderBean> datas) {
        mData.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mData != null) {
            ResHotPeopleInfo.HotPeopleOrderBean str = mData.get(position);
            ((HotPeopleViewHolder) holder).init(str);
        }
//        holder.textView.setText(str);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    public class HotPeopleViewHolder extends RecyclerView.ViewHolder {

        TextView mTvGroupBuyTitle;
        TextView mTvGroupBuyTime;
        TextView mTvLeast;
        EditText mTvGroupBuyDes;
        TextView mTvGroupBuyMoney;

        TextView mTvFollowNumber;

        TextView mTvFollow;

        TextView mRlAwardNumber, tv_jiangjin, tv_danbeijine;
        RelativeLayout mRlAwardBg;
        View view;
        RelativeLayout rl_bei;
        ImageView iv_type;
        TextView item_hotpeople_total_money;
        LinearLayout beizhu;

        public HotPeopleViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            mTvGroupBuyTitle = itemView.findViewById(R.id.tv_group_buy_title);
            iv_type = itemView.findViewById(R.id.iv_type);
            mTvGroupBuyTime = itemView.findViewById(R.id.tv_group_buy_time);
            mTvLeast = itemView.findViewById(R.id.tv_least);
            mTvGroupBuyDes = itemView.findViewById(R.id.tv_group_buy_des);
            mTvGroupBuyMoney = itemView.findViewById(R.id.tv_group_buy_money);
            mTvFollowNumber = itemView.findViewById(R.id.tv_follow_number);
            mTvFollow = itemView.findViewById(R.id.tv_follow);
            mRlAwardNumber = itemView.findViewById(R.id.rl_award_number);
            mRlAwardBg = itemView.findViewById(R.id.rl_award_bg);
            rl_bei = itemView.findViewById(R.id.rl_bei);
            item_hotpeople_total_money = itemView.findViewById(R.id.item_hotpeople_total_money);
            tv_jiangjin = itemView.findViewById(R.id.tv_jiangjin);
            tv_danbeijine = itemView.findViewById(R.id.tv_danbeijine);
            beizhu = itemView.findViewById(R.id.beizhu);
        }

        public void init(final ResHotPeopleInfo.HotPeopleOrderBean hotPeopleOrder) {
//            mTvGroupBuyTitle.setText(hotPeopleOrder.getLotteryname());
            mTvGroupBuyTitle.setText(Html.fromHtml("已跟单<font color='red'>" + hotPeopleOrder.getFollow_winmoneys_total() + "</font>元"));
            Date parse = null;
            try {
                parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(hotPeopleOrder.getEndtime());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            String endTime = new SimpleDateFormat("yyyy-MM-dd").format(parse);
            if (TextUtils.isEmpty(endTime)) {
                endTime = hotPeopleOrder.getEndtime();
            }
            switch (hotPeopleOrder.getClassid()) {
                case 5:
                    //足球
                    iv_type.setBackgroundResource(R.drawable.zuqiu);
                    break;
                case 6:
                    //蓝球
                    iv_type.setBackgroundResource(R.drawable.lanqiu);
                    break;
            }
            mTvGroupBuyTime.setText(endTime);
            beizhu.setVisibility(View.GONE);
//            mTvLeast.setText(hotPeopleOrder.getFollow_minmoney() + "元起投");
//            mTvGroupBuyDes.setText(hotPeopleOrder.getTitle());
            mTvGroupBuyDes.setText(1 + "");
            mTvGroupBuyDes.setTag(1);
            item_hotpeople_total_money.setText(Integer.parseInt(mTvGroupBuyDes.getText().toString()) * Float.parseFloat(mData.get(getAdapterPosition()).getFollow_minmoney()) + "元");
            mTvGroupBuyDes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showInputDialog((EditText) view, item_hotpeople_total_money, getAdapterPosition());
                }
            });
            mTvGroupBuyMoney.setText(hotPeopleOrder.getOne_money() + "元");
            mTvFollowNumber.setText(hotPeopleOrder.getFollow_count() + "人");
            tv_jiangjin.setText(hotPeopleOrder.getWin_money() + "元");
            rl_bei.setVisibility(View.VISIBLE);
            tv_danbeijine.setText(hotPeopleOrder.getFollow_minmoney() + "元");
            if ("1".equals(hotPeopleOrder.getState())) {
                mRlAwardBg.setVisibility(View.VISIBLE);
                //已撤单
                mTvFollow.setVisibility(View.INVISIBLE);
                mRlAwardBg.setBackgroundResource(R.drawable.follow_no_win_img);
                mTvFollow.setOnClickListener(null);
                mRlAwardNumber.setVisibility(View.INVISIBLE);
            } else if ("2".equals(hotPeopleOrder.getState())) {
                //未中奖
                mRlAwardBg.setVisibility(View.VISIBLE);
                mRlAwardBg.setBackgroundResource(R.drawable.follow_no_win_img);
                mTvFollow.setVisibility(View.INVISIBLE);
                mRlAwardNumber.setVisibility(View.INVISIBLE);
                mTvFollow.setOnClickListener(null);
            } else if ("3".equals(hotPeopleOrder.getState())) {
                //已经中奖
                mRlAwardBg.setVisibility(View.VISIBLE);
                mRlAwardBg.setBackgroundResource(R.drawable.follow_win);
                mRlAwardNumber.setVisibility(View.VISIBLE);
                mRlAwardNumber.setText("￥" + hotPeopleOrder.getWin_money());
                mTvFollow.setVisibility(View.INVISIBLE);
                mTvFollow.setOnClickListener(null);

                mTvFollow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //这里要执行的逻辑就是立即跟单
                        context.startActivity(HotOrderDetailActivity.getIntent(context, hotPeopleOrder.getId()).putExtra("beishu", mTvGroupBuyDes.getTag().equals("") ? 0 : Integer.valueOf(mTvGroupBuyDes.getTag().toString())));
                    }
                });
            } else {
                //这里表示是可跟单；
                mRlAwardBg.setVisibility(View.INVISIBLE);

                mRlAwardNumber.setVisibility(View.INVISIBLE);

                if (hotPeopleOrder.getCan_follow() > 0) {
                    mTvFollow.setVisibility(View.VISIBLE);

                    mTvFollow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //这里要执行的逻辑就是立即跟单
                            context.startActivity(HotOrderDetailActivity.getIntent(context, hotPeopleOrder.getId()).putExtra("beishu", mTvGroupBuyDes.getTag().equals("") ? 0 : Integer.valueOf(mTvGroupBuyDes.getTag().toString())));
                        }
                    });

                } else {
                    mTvFollow.setVisibility(View.INVISIBLE);
                    mTvFollow.setOnClickListener(null);
                }
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = HotOrderDetailActivity.getIntent(context, hotPeopleOrder.getId());
                    context.startActivity(intent);
                }
            });

        }
    }

    private void showInputDialog(final EditText ed, final TextView item_hotpeople_total_money, final int position) {
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("输入倍数");
        View view = View.inflate(context, R.layout.alert_x, null);
        final EditText editText = view.findViewById(R.id.edit_content);
        editText.setText(ed.getText());
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.setHint("请输入倍数");
        alert.setView(view);
        alert.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ed.setText(editText.getText());
                ed.setTag(editText.getText());
                item_hotpeople_total_money.setText(Integer.parseInt(editText.getText().toString()) * Float.parseFloat(mData.get(position).getFollow_minmoney()) + "元");
            }
        });
        alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        alert.create().show();
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
